#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化并设置欢迎界面
    ui->WelcomeLabel->setText(
                QString("<h1 style='color:green'>猜数字游戏</h1>"));
    ui->stackedWidget->setCurrentWidget(ui->welcomePage);
    ui->enterGameButton->installEventFilter(this);
    ui->leaveGameButton->installEventFilter(this);
    ui->mainToolBar->setMovable(false);
    this->setWindowTitle("猜数字游戏！");
    //初始化并设置游戏界面
    ui->labelGuessResult->setAlignment(Qt::AlignCenter);
    ui->textEditGuessShow->setEnabled(false);
    ui->textEditGuessShow->setAlignment(Qt::AlignCenter);

    //给按钮绑定事件
    connect(ui->pushButton0,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton1,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton2,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton3,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton4,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton5,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton6,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton7,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton8,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButton9,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButtonDelete ,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
    connect(ui->pushButtonSubmit ,&QPushButton::clicked ,this,&MainWindow::handleNumberButtonPress );
}
void MainWindow::handleNumberButtonPress()
{
    if (gameOver) {
        //如果游戏结束则不处理
        return;
    }
    QPushButton* sendButton =static_cast<QPushButton*>(this->sender());
    QString qString = sendButton->text();

    if (qString=="0" && guessText.size()==0 ) {
        return;
    }
    if (guessText.size()<=4) {
        if (sendButton==ui->pushButtonSubmit) {
            if (guessText.size()<4) {
                return;
            }
            //猜出的数字
            int guessInt = guessText.toUInt();
            QString theGuessText;
            if (guessInt<random) {
                theGuessText = QString("<h1 style='color:red'>数字：%1 过小!</h1>")
                        .arg(guessInt);
            }else if (guessInt>random) {

                theGuessText = QString("<h1 style='color:red'>数字：%1 过大!</h1>")
                        .arg(guessInt);

            }else {
                theGuessText = QString("<h1 style='color:green'>恭喜你猜对了 %1 !</h1>")
                        .arg(guessInt);
                this->killTimer(remainingTimeId);
                remainingTimeId=-1;
                animationTimeId = this->startTimer(AnimationTime);
                gameOver=true;
            }
            qDebug()<<"toPlainText:"<<ui->textEditGuessShow->toPlainText();
            ui->labelGuessResult->setText(theGuessText);
            ui->textEditGuessShow->clear();
            guessText.clear();
            return;
        } else if (sendButton==ui->pushButtonDelete) {
            if (guessText.size()<=1) {
                guessText.clear();
            }else {
                guessText.chop(1);
            }
        }else {
            if (guessText.size()<4) {
                guessText+=qString;
            }
        }
        ui->textEditGuessShow->setHtml("<center><h1>"+guessText+"</h1></center>");
    }

}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::timerEvent(QTimerEvent* qte)
{
    if (qte->timerId()==remainingTimeId) {
        //获取剩余时间
        int remainingTime = ui->progressBar->value();
        remainingTime--;
        if (remainingTime) {
            ui->progressBar->setValue(remainingTime);
            qDebug()<<"comboBox:"<<remainingTime;
        }else {
            //结束定时器
            this->killTimer(remainingTimeId);
            //进入失败页面
            ui->stackedWidget->setCurrentWidget(ui->gameOver);

            QMovie* pMovie = new QMovie(QString(":/new/gif/e2.gif"));

            ui->labelGameOverGif->setMovie(pMovie);
            ui->labelGameOverText->setText(QString("<h1>游戏失败！</h1>"));
            pMovie->start();
            //5秒后重新进入游戏欢迎界面
            animationTimeId = this->startTimer(AnimationTime);

        }
    }else if (qte->timerId()==animationTimeId) {
        //重新进入游戏欢迎界面
        ui->stackedWidget->setCurrentWidget(ui->welcomePage);
        this->killTimer(animationTimeId);
    }
}

bool MainWindow::confirmClose(QCloseEvent* qce)
{
    int ret =  QMessageBox::question(
                this
                ,"提示"
                ,"确认要关闭吗？"
                ,QMessageBox::Yes|QMessageBox::Cancel
                ,QMessageBox::Yes
                );
    if (ret==QMessageBox::Yes) {
        qDebug()<<"你输入的是 YES";
        qce->accept();
        ::TerminateProcess(::GetCurrentProcess(), 0);
    }else {
        qDebug()<<"你输入的是 Cancel";
        qce->ignore();
    }
    return true;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
    confirmClose(event);
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event) ;
    if (watched==ui->enterGameButton) {
        if (mouseEvent->type()==QEvent::MouseButtonPress && mouseEvent->button()==Qt::LeftButton) {
            //获取进入游戏的设置时间
            int comboBox = ui->comboBox->currentText().toInt();

            if (ui->comboBox->currentIndex()==0)
            {
                QMessageBox::warning(this, tr("My Application"),
                                     tr("请选择时间"),
                                     NULL,//显示哪些按钮
                                     NULL);
                return true;
            }

            //设置进度条时间
            ui->progressBar->setMinimum(0);
            ui->progressBar->setMaximum(comboBox);
            ui->progressBar->setValue(comboBox);
            //启动定时器,计算游戏时间
            remainingTimeId = this->startTimer(RemainingTime);
            guessText.clear();
            ui->textEditGuessShow->setHtml("<center><h1>"+guessText+"</h1></center>");

            ui->labelGuessResult->setText("<h1 style='color:gray'>赶快竞猜吧!(竞猜数字为4位)</h1>");

            qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
            while ((random=(qrand()%10000))<999)
                qDebug()<<"随机数："<<random;
            qDebug()<<"随机数："<<random;
            gameOver = false;
            ui->stackedWidget->setCurrentWidget(ui->gamePage);
            return true;
        }
    }else if (watched==ui->leaveGameButton) {
        if (mouseEvent->type()==QEvent::MouseButtonPress && mouseEvent->button()==Qt::LeftButton) {
            confirmClose(static_cast<QCloseEvent*>(event));
            return true;
        }
    }
    return QWidget::eventFilter(watched,event);
}
